﻿--phantom
ALTER PROC sp_xemTacGia @tenTacGia nvarchar(31)
AS
BEGIN tran
	set tran isolation level serializable
	DECLARE @tongsp int, @matuasach int
	BEGIN TRY                
                DECLARE cursor_name CURSOR DYNAMIC FOR
                SELECT ts.ma_tuasach FROM TuaSach ts WHERE ts.tacgia=@tenTacGia
                
                OPEN cursor_name;
				SET @tongsp = (SELECT COUNT(*) FROM TuaSach ts WHERE ts.tacgia=@tenTacGia )				
	
				PRINT N'Tổng số sách:' + CAST(@tongsp AS char(3))
				PRINT N'_________________________________________'

                FETCH NEXT FROM cursor_name INTO @matuasach;
                
                WHILE @@FETCH_STATUS = 0
                BEGIN              
					PRINT @matuasach
					WAITFOR DELAY '00:00:01'
                FETCH NEXT FROM cursor_name INTO @matuasach;
                
                END
                
                CLOSE cursor_name;
                DEALLOCATE cursor_name;      
    END TRY
	BEGIN CATCH
		declare @ErrorMsg varchar(2000)
		select @ErrorMsg = N'Lỗi: ' + ERROR_MESSAGE()
		raiserror(@ErrorMsg, 16,1)
		rollback tran
		return
	END CATCH
COMMIT TRAN


--EXEC sp_xemTacGia 'Pearl S. Buck'